# 1. 列表定义
li = [1,'1',[1,2,3]]

print(li,type(li)) # [1, '1', [1, 2, 3]] <class 'list'>
print(li[1:]) #['1', [1, 2, 3]]
for e in li:
    print(e)
# 1
# 1
# [1, 2, 3]

# 2. 相关操作
## 2.1 添加元素
### (1)append
li1 = ['one','two','three']
li1.append('five')
li1.extend("six") # (拆出来)
print(li1) # ['one', 'two', 'three', 'five', 's', 'i', 'x']

li1.insert(3,"000")
print(li1)  # ['one', 'two', 'three', '000', 'five', 's', 'i', 'x']
li1.insert(100,"aaa")
print(li1) # ['one', 'two', 'three', '000', 'five', 's', 'i', 'x', 'aaa']

## 2.2 修改元素 (直接通过下标进行修改)

## 2.3 查找元素
# (1)in 和 not in
# (2)index
# (3) count

## 2.4 删除元素
# (1) del
li2 = ['a','b','c','d']
del li2
# print(li2)
# Traceback (most recent call last):
#   File "E:\python代码库\python-learn\python_base\数据容器\list_learn.py", line 36, in <module>
#     print(li2)
#           ^^^
# NameError: name 'li2' is not defined. Did you mean: 'li'?
li2 = ['a','b','c','d']
del li2[3]
print(li2) # ['a', 'b', 'c']

# (2) pop 删除指定下标,不指定删除最后一个元素
li2 = ['a','b','c','d']
li2.pop()
print(li2) # ['a', 'b', 'c']
li2.pop(0)
print(li2) # [ 'b', 'c']

# (3) remove 根据元素的值进行删除
li2 = ['a','b','c','d']
li2.remove('c')
print(li2) # ['a', 'b', 'd']
li2 = ['a','b','c','d','a','a']
li2.remove('a')
print(li2) # ['b', 'c', 'd', 'a', 'a']

# 2.5 排序
# (1) sort
li3 = [1,43,65,76,83,324]
li3.sort() # [1, 43, 65, 76, 83, 324]
print(li3)
li3.reverse()
print(li3) # [324, 83, 76, 65, 43, 1] -> 两个一起用可以达到倒序效果

# 2.6 列表推导式
# (1) [表达式 for 变量 in 列表]
li4 = [1,2,3,4,5,6]
[print(i*5,end='\t') for i in li4] # 5	10	15	20	25	30
li5 = [i for i in range(1,10)]
print(li5) # [1, 2, 3, 4, 5, 6, 7, 8, 9]

# (2)   添加条件
li6 = [i for i in range(1,20) if i % 2 == 1]
print(li6) # [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]


